<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="tabName">
          <el-tab-pane label="登录账户设置" name="first">
            <!-- 登录账户设置--表单 -->
            <el-form :model="userInfo" :rules="UserInfoRules" ref="userInfoRef" label-width="120px"
              style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input v-model="userInfo.password2" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">保存修改</el-button>
                <el-button @click="$router.back()">返回</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情" name="second">
            <!-- 打印按钮 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${userId}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <!-- <user-info></user-info> -->
            <!-- 使用 component标签，它可以是任意组件,动态 -->
            <component :is="userComponent"></component>
          </el-tab-pane>
          <el-tab-pane label="岗位信息" name="third">
            <!-- 打印按钮 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印岗位信息">
                <router-link :to="`/employees/print/${userId}?type=job`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <!-- <job-info></job-info> -->
            <component :is="jobComponent"></component>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
// API
import { getUserDetailById } from "@/api/user";
import { saveUserDetailById } from "@/api/employees";
// 组件
import UserInfo from "./components/user-info.vue";
import JobInfo from "./components/job-info.vue";

export default {
  components: {
    UserInfo,
    JobInfo,
  },
  data() {
    return {
      // 定义组件名称，配合 component标签 一起使用，改成 user-info 也可以
      userComponent: "UserInfo",
      jobComponent: "job-info",
      // 被激活的标签页名字
      tabName: "first",
      // 详情页面的员工id
      userId: this.$route.params.id,
      //   表单数据
      userInfo: {
        username: "",
        password2: "", //后端传过来的密码是加密后的，如果不做修改直接进行保存，会超出长度
      },
      //   表单校验对象
      UserInfoRules: {
        username: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
        ],
        password2: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 9, message: "密码长度为6-9位", trigger: "blur" },
        ],
      },
    };
  },
  created() {},
  mounted() {
    // 请求用户数据
    this.getUserDetailById(this.userId);
  },
  methods: {
    // 获取用户信息
    async getUserDetailById(userId) {
      this.userInfo = await getUserDetailById(userId);
    },
    // 保存用户信息
    saveUser() {
      this.$refs.userInfoRef.validate(async (valid) => {
        if (valid) {
          try {
            await this.$confirm("确定要修改用户信息吗？");
            this.userInfo.password = this.userInfo.password2;
            await saveUserDetailById(this.userInfo);
            this.$message.success("修改用户信息成功");
          } catch (error) {
            console.log(error);
            // this.$message.error({ message: error || "存在错误" });
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
</style>